package com.monitor.zk.zookeeper.command;

import bean.VMMemory;
import com.monitor.zk.zookeeper.util.FormatSize;
import lombok.extern.slf4j.Slf4j;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.ThreadMXBean;
import java.text.DecimalFormat;
import java.util.Date;

@Slf4j
/**
 * 当前程序的vm使用信息
 */
public class VMInfoCommand {

    //内存
    private MemoryMXBean memoryMXBean;
    //线程（后续再加）
    private ThreadMXBean threadMXBean;

    public VMInfoCommand(){
        memoryMXBean = ManagementFactory.getMemoryMXBean();
        threadMXBean = ManagementFactory.getThreadMXBean();
    }

    public VMMemory vmMemory(){
        VMMemory vmMemory = new VMMemory();
        vmMemory.setSearchTime(new Date());
        MemoryUsage usage = memoryMXBean.getHeapMemoryUsage();
        vmMemory.setMaxMemory(FormatSize.formSize(usage.getMax()+"byte"));
        vmMemory.setTotalMemory(FormatSize.formSize(usage.getInit()+"byte"));
        vmMemory.setUseMemory(FormatSize.formSize(usage.getUsed()+"byte"));
        return vmMemory;
    }
}
